import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DatePickerPage extends StatefulWidget {
  @override
  _DatePickerPageState createState() => _DatePickerPageState();
}

class _DatePickerPageState extends State<DatePickerPage> {
  int selectedYear = 2022;
  int selectedMonth = 6;
  int selectedDay = 18;

  final List<int> years = List.generate(10, (i) => 2020 + i); // 2020~2029
  final List<int> months = List.generate(12, (i) => i + 1);
  final List<int> days = List.generate(31, (i) => i + 1);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          height: 200,
          color: Colors.grey[200],
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // 年份选择
              Expanded(
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(
                    initialItem: years.indexOf(selectedYear),
                  ),
                  itemExtent: 40,
                  onSelectedItemChanged: (index) {
                    setState(() {
                      selectedYear = years[index];
                    });
                  },
                  children: years.map((y) => Center(child: Text('$y年'))).toList(),
                ),
              ),
              // 月份选择
              Expanded(
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(
                    initialItem: months.indexOf(selectedMonth),
                  ),
                  itemExtent: 40,
                  onSelectedItemChanged: (index) {
                    setState(() {
                      selectedMonth = months[index];
                    });
                  },
                  children: months.map((m) => Center(child: Text('$m月'))).toList(),
                ),
              ),
              // 日期选择
              Expanded(
                child: CupertinoPicker(
                  scrollController: FixedExtentScrollController(
                    initialItem: days.indexOf(selectedDay),
                  ),
                  itemExtent: 40,
                  onSelectedItemChanged: (index) {
                    setState(() {
                      selectedDay = days[index];
                    });
                  },
                  children: days.map((d) => Center(child: Text('$d日'))).toList(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
